let shoppingCar=wx.getStorageSync("shoppingCar") || {items:[]}
module.exports={
   addShopping(pid){
      let pos=shoppingCar.items.findIndex(item=>{
        return item.pid===pid
      })
      // console.log(pos)
      if(pos<0){
        shoppingCar.items.push({pid:pid,count:1})
      }
      else{
        shoppingCar.items[pos].count++
      }
     wx.setStorageSync("shoppingCar", shoppingCar)
   },
   incress(pid){
     let pos = shoppingCar.items.findIndex(item => {
       return item.pid === pid
     })
     shoppingCar.items[pos].count++
     wx.setStorageSync("shoppingCar", shoppingCar)
   },
   decress(pid){
     let pos = shoppingCar.items.findIndex(item => {
       return item.pid === pid
     })
     if (shoppingCar.items[pos].count>1){
       shoppingCar.items[pos].count--
       wx.setStorageSync("shoppingCar", shoppingCar)
     }
   },
getCount(){
  let count=0
  for (let item of shoppingCar.items){
    count+=item.count
  }
  return count
}
}